package get.newsdaily;


import get.newsdaily.adapters.CategoryAdapter;
import get.newsdaily.model.RssItem;
import get.newsdaily.utils.RssParser;
import get.newsdaily.utils.variables;

import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

/**
 * @author Win 8.1 VS8 X64
 *
 */
public class CategoryActivity extends ListActivity {
	private ProgressDialog dialog;
	int paper;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		Bundle bundle = getIntent().getExtras(); //get intent đã định nghĩa
		int paper = bundle.getInt(variables.PAPER);
		setTitle(variables.PAPERS[paper]);
		
		CategoryAdapter adapter = new CategoryAdapter(this, variables.ICONS[paper], variables.CATEGORIES[paper]);
		setListAdapter(adapter);
	}

	
// @Override protected void onListItemClick(ListView l, View v, int
//	  position, long id) { super.onListItemClick(l, v, position, id);
//	  
//	  try{ super.onListItemClick(l, v, position, id);
//	  
//	  Intent intent = new Intent(CategoryActivity.this,
//	  NewsListActivity.class);
//	  
//	  intent.putExtra(variables.PAPER, paper); // Lấy lại thằng ở trên
//	  intent.putExtra(variables.CATEGORY, position);
//	  
//	  startActivity(intent); int key = paper * 1000 + position; RssParser
//	  parser = new RssParser();
//	  
//	  List<RssItem> items =
//	  parser.getNewsList(variables.LINKS[paper][position]);
//	  
//	  variables.newsmap.put(key, items);
//	  
//	  } catch(Exception e){ Toast.makeText(v.getContext(),"Lỗi: " +
//	  e,Toast.LENGTH_LONG).show(); Log.w("lỗi khởi động", e.toString()); }
//	  
//	  
//	  }
			
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		int key = paper*1000+position;
		if(variables.newsmap.get(key)==null) {
		dialog = ProgressDialog.show(this, " ","Loading "+ variables.CATEGORIES[paper][position]);
		new CategoryAsysTask().execute(position);}
		
		else {
			dialog.dismiss();
			Intent intent = new Intent(CategoryActivity.this, NewsListActivity.class);
			intent.putExtra(variables.PAPER, paper);
			intent.putExtra(variables.CATEGORY,position);
			startActivity(intent);
		
		}
}

class CategoryAsysTask extends AsyncTask<Integer, Void, Void> {
int position;
	@Override//gọi trong quá trình RunTime
	protected Void doInBackground(Integer... params) {
		position = params[0];
		try {
			
			RssParser parser = new RssParser();
			int key = paper*1000+position;
					List<RssItem>	items = parser.getNewsList(variables.LINKS[paper][position]);
					variables.newsmap.put(key, items);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					
	}
		return null;
	}
	@Override
			protected void onPostExecute(Void result) {
				if(dialog != null){
					dialog.dismiss();
				}
				Intent intent = new Intent(CategoryActivity.this, NewsListActivity.class);
				intent.putExtra(variables.PAPER, paper);
				intent.putExtra(variables.CATEGORY,position);
				startActivity(intent);
				super.onPostExecute(result);
			}
	
}
}

